﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceManager.Services;
using System.Collections;

namespace QuotationService.RealtimeQuotation
{
    internal class QuotationPublishServiceContainer
    { 
        /// <summary>
        /// 发布者
        /// </summary>
        private static Dictionary<QuotationSource, QuotationPublishService> Publishers = new Dictionary<QuotationSource, QuotationPublishService>();

        /// <summary>
        /// 允许与每个行情查询服务器建立多少个连接
        /// 如果查询服务设置为使用发布服务器，则会共享这些连接数
        /// </summary>
        internal static Int32 ConnNumPerService = 10;

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="connNumPerService">允许与每个行情查询服务器建立多少个连接</param>
        public static void Initialize(int connNumPerService = 10)
        {
            ConnNumPerService = connNumPerService;
        }

        /// <summary>
        /// 获取指定行情源的行情发布服务对象
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        internal static QuotationPublishService GetPublishService(QuotationSource source)
        {
            QuotationPublishService qps = null;
            lock (((ICollection)Publishers).SyncRoot)
            {
                if (Publishers.TryGetValue(source, out qps))
                    return qps;

                qps = DirectoryService.GetQuotationPublishService(QuotServiceNames.PubServicePrefix + source, ConnNumPerService);
                Publishers[source] = qps;
                return qps;
            }
        }

    }
}
